package com.zhoug.common.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.common.R;
import com.zhoug.common.prodivers.AppContextProvider;


/**
 * Toast 工具 可以在主线程和子线程中使用
 * 支持改变文字颜色和背景颜色
 *
 * @Author HK-LJJ
 * @Date 2019/10/14
 * @Description TODO
 */
public class ToastUtils {
    private static Toast lastToast;

    private static Context appContext;

    public static void init(Context appContext){
        ToastUtils.appContext=appContext;
    }


    public static Context getAppContext(){
        if(appContext==null){
            synchronized (ContactsUtils.class){
                if(appContext==null){
                    appContext=AppContextProvider.getAppContext();
                }
            }
        }
        return appContext;
    }

    /**
     * 创建toast
     * @param context
     * @param msg
     * @param duration
     * @param cancelLast 是否关闭上一个toast
     * @return
     */
    private static Toast makeToast(Context context, String msg, int duration, boolean cancelLast) {
        if (cancelLast && lastToast != null) {
            lastToast.cancel();
            lastToast = null;
        }
        lastToast = new Toast(context);
        LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.common_layout_toast, null);
        TextView textView = v.findViewById(R.id.toast_message);
        textView.setText(msg);
        lastToast.setView(v);
        lastToast.setDuration(duration);
        return lastToast;
    }


    private static Toast makeToast(Context context, String msg, int duration) {
        return makeToast(context, msg, duration, true);
    }



    public static void toastShort(Context context, Object msg) {
//        Toast.makeText(context, msg == null ? "null" : msg.toString(), Toast.LENGTH_SHORT).show();
        makeToast(context, msg == null ? "null" : msg.toString(), Toast.LENGTH_SHORT).show();
    }

    public static void toastShort( Object msg) {
        toastShort(getAppContext(), msg);
    }


    public static void toastLong(Context context, Object msg) {
//        Toast.makeText(context, msg == null ? "null" : msg.toString(), Toast.LENGTH_LONG).show();
        makeToast(context, msg == null ? "null" : msg.toString(), Toast.LENGTH_LONG).show();

    }

    public static void toastLong( Object msg) {
        toastLong(getAppContext(), msg);
    }


    public static void toastShortCenter(Context context, Object msg) {
        Toast toast = makeToast(context, msg == null ? "null" : msg.toString(), Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public static void toastShortCenter( Object msg) {
        toastShortCenter(getAppContext(), msg);
    }

    public static void toastLongCenter(Context context, Object msg) {
        Toast toast = makeToast(context, msg == null ? "null" : msg.toString(), Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public static void toastLongCenter( Object msg) {
        toastLongCenter(getAppContext(), msg);
    }

}
